package com.psg.service.impl;

import com.psg.dao.AddressDao;
import com.psg.dao.UserDao;
import com.psg.exception.RuleDoesNotMeetException;
import com.psg.pojo.Address;
import com.psg.pojo.User;
import com.psg.service.AddressService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class AddressServiceImpl implements AddressService {

    @Resource
    private AddressDao addressDao;
    @Resource
    private UserDao userDao;

    @Override
    public void save(Address address) {
        if (null != address.getId()) throw new RuleDoesNotMeetException("参数异常！");
        User user = userDao.findById(address.getUserId());
        if(null == user) throw new RuleDoesNotMeetException("参数异常！");
        address.setUserId(user.getuId());
        if (null == address) address.setDefaultAddress(false);
        if (null == address.getDefaultAddress()) {
            address.setDefaultAddress(false);
        }

        if (address.getDefaultAddress()) {
            Address defaultAddress = addressDao.queryDefaultAddress(user.getuId());
            if (null != defaultAddress) {
                defaultAddress.setDefaultAddress(false);
                addressDao.updateAddress(defaultAddress);
            }
        }
        addressDao.insert(address);
    }

    @Override
    public List<Address> findAllAddressByUserId(Integer userId) {
        return addressDao.queryAllAddressByUserId(userId);
    }

    @Override
    public void update(Address address) {
        if (null == address.getId()) throw new RuleDoesNotMeetException("参数异常！");
        User user = userDao.findById(address.getUserId());
        if(null == user) throw new RuleDoesNotMeetException("参数异常！");
        if (null == address) address.setDefaultAddress(false);
        if (null == address.getDefaultAddress()) {
            address.setDefaultAddress(false);
        }
        if (address.getDefaultAddress()) {
            Address defaultAddress = addressDao.queryDefaultAddress(user.getuId());
            if (null != defaultAddress) {
                defaultAddress.setDefaultAddress(false);
                addressDao.updateAddress(defaultAddress);
            }
        }
        addressDao.updateAddress(address);
    }

    @Override
    public Address findAddressById(int addressId) {
        return addressDao.queryById(addressId);
    }

    @Override
    public void changeDefault(int addressId, Integer userId) {
        Address address = addressDao.queryById(addressId);
        if (null==address) throw new RuleDoesNotMeetException("要操作的地址信息不存在！");
        address.setDefaultAddress(true);
        List<Address> addresses = addressDao.queryAllAddressByUserId(userId);
        Address oldDefaultAddress = addressDao.queryDefaultAddress(userId);
        oldDefaultAddress.setDefaultAddress(false);
        addressDao.updateAddress(oldDefaultAddress);
        addressDao.updateAddress(address);
    }
}
